package com.chzc.fm.duotin.ui.home.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.home.PlayList2Data;
import com.chzc.fm.duotin.ui.home.PlayList2Event;
import com.chzc.fm.duotin.ui.other.BaseFragment;
import com.chzc.fm.duotin.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;


/**
 * Created by Administrator on 2016/6/29.
 */
@SuppressLint("ValidFragment")
public class FragmentPlayList2 extends BaseFragment {

    private List<PlayList2Data.ContentListBean.DataListBean> dataList;
    private ListView listView;
    private PlayListAdapter  playListAdapter;

    @SuppressLint("ValidFragment")
    public FragmentPlayList2(List<PlayList2Data.ContentListBean.DataListBean> dataList) {

        this.dataList = dataList;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_layout_playlist;
    }

    @Override
    protected void initViews() {

        //下载界面的listview
        listView = (ListView) root.findViewById(R.id.listView);
        //当没有数据时显示的默认视图
        listView.setEmptyView(root.findViewById(R.id.defaults_show));
        playListAdapter = new PlayListAdapter(getContext(),dataList);
        listView.setAdapter(playListAdapter);
        // listView.setOnItemClickListener(getContext());

        EventBus.getDefault().register(this);

    }

    @Subscribe
    public void onEventMainThread(PlayList2Event event){
        LogUtil.e("FragmentPlayList2 这里执行了吗---");
        playListAdapter.notifyDataSetChanged();

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    //节目列表界面的adapter

    public class PlayListAdapter extends BaseAdapter {

        //设置数据
        List<PlayList2Data.ContentListBean.DataListBean> dataList;
        //
        LayoutInflater inflater;

        public PlayListAdapter(Context context, List<PlayList2Data.ContentListBean.DataListBean> dataList) {
            //设置数据
            inflater = LayoutInflater.from(context);
            this.dataList = dataList;
        }

        @Override
        public int getCount() {
            return dataList==null?0:dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHoder holder = null;
            //判断是否缓存
            if (convertView==null){
                holder = new ViewHoder();
                //通过LayoutInflater实例化
                convertView = inflater.inflate(R.layout.activity_pull_listview_playlist,null);
                holder.tvTitle = (TextView)convertView.findViewById(R.id.tv_title);
                holder.tvCount = (TextView)convertView.findViewById(R.id.tv_count);
                holder.tvTime = (TextView)convertView.findViewById(R.id.tv_time);

                convertView.setTag(holder);
            }else{
                //通过tag找到缓存的布局
                holder = (ViewHoder) convertView.getTag();
            }

            //设置布局中控件要显示的视图

            holder.tvTitle.setText(dataList.get(position).getTitle());
            holder.tvCount.setText(""+dataList.get(position).getPlay_num());
            holder.tvTime.setText(dataList.get(position).getDuration());
      //      LogUtil.e(" 1 "+dataList.get(position).getTitle()+" 2 "+dataList.get(position).getPlay_num()+" 3 "+dataList.get(position).getDuration());

            return convertView;
        }

        public final class ViewHoder{

            public TextView tvTitle,tvCount,tvTime;

        }
    }
}
